#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QFile creat(".\\recode.txt");
    if(!creat.exists())
    {
        qDebug()<<"文件不存在";
        creat.open(QIODevice::WriteOnly);
        creat.write("昵称\t时间\n");
    }
    creat.close();

    this->qtime = new QTimer;
    this->m_time = 0;
    this->map=new Board;
    connect(qtime,&QTimer::timeout,[=](){
        this->m_time++;
    });
    //配置主窗口
    this->setFixedSize(256,320);
    QPixmap WindowPix;
    WindowPix.load(":/res/bomb.png");
    this->setWindowIcon(QIcon(WindowPix));
    this->setWindowTitle("扫雷");

    //创建菜单栏
    QMenuBar* mainMenuBar = menuBar();
    setMenuBar(mainMenuBar);
    QMenu* gameMenu = mainMenuBar->addMenu("游戏");
    QAction* showAction = new QAction("查看记录");
    gameMenu->addAction(showAction);
    QAction* clearAction = new QAction("清空记录");
    gameMenu->addAction(clearAction);
    gameMenu->addSeparator();
    QAction* exitAction = new QAction("退出");
    gameMenu->addAction(exitAction);

    QMenu* helpMenu = mainMenuBar->addMenu("帮助");
    QAction* helpAction = new QAction("游戏玩法");
    helpMenu->addAction(helpAction);

    //点击退出，离开游戏
    connect(exitAction,&QAction::triggered,[=](){
        this->close();
    });
    //点击查看记录，展示记录
    connect(showAction,&QAction::triggered,[=](){
        showWdiget = new ShowWidget;
        showWdiget->show();
    });
    //点击清空记录,清空记录
    connect(clearAction,&QAction::triggered,[=](){
        if(QMessageBox::Yes == QMessageBox::question(this,"清空记录","是否清空"))
        {
            QFile file(".\\recode.txt");
            file.open(QIODevice::WriteOnly);
            file.write("昵称\t时间\n");
            file.close();
        }
    });
    //点击游戏玩法，显示玩法
    connect(helpAction,&QAction::triggered,[=](){
        QMessageBox::information(this,"游戏玩法",
                                 "点击开始按钮进入游戏\n左键点击:翻开这个位置的区域\r\n右键点击:标记为炸弹");
    });


    //开始按钮创建和放置
    startBtn = new StartPushButton(":/res/doge.png",":/res/vegetable.png");
    startBtn->setParent(this);
    startBtn->move((this->width()-startBtn->width())*0.5,this->height()*0.18-startBtn->height());

    //开始按钮按下后产生棋盘,更新按钮,开始计数
    connect(startBtn,&QPushButton::released,[=](){
        if(this->map!=NULL)
        {
            delete  this->map;
        }
        this->map = new Board;

        for(int i = 0;i < 10;i++)
        {
            for(int j = 0;j < 10;j++)
            {
                this->mineBoard[i][j]->update(map);
            }
        }
        qtime->start(100);


    });

    //放置游戏按钮
    for(int i = 0;i < 10;i++)
    {
        for(int j = 0;j < 10;j++)
        {
            UnknownPushButton* uBtn = NULL;
            if(this->map->isMine(i,j) == true)
            {
                uBtn = new UnknownPushButton(":/res/bomb.png");
            }
            else
            {
                int mineNum=0;//统计周围雷数
                if(i==0&&j==0)
                    mineNum=map->isMine(i+1,j)+map->isMine(i+1,j+1)+map->isMine(i,j+1);
                else if(i==0&&j==9)
                    mineNum=map->isMine(i+1,j)+map->isMine(i+1,j-1)+map->isMine(i,j-1);
                else if(i==0)
                    mineNum=map->isMine(i,j-1)+map->isMine(i,j+1)+map->isMine(i+1,j+1)+map->isMine(i+1,j)+map->isMine(i+1,j-1);
                else if(i==9&&j==0)
                    mineNum=map->isMine(i-1,j)+map->isMine(i-1,j+1)+map->isMine(i,j+1);
                else if(i==9&&j==9)
                    mineNum=map->isMine(i-1,j)+map->isMine(i-1,j-1)+map->isMine(i,j-1);
                else if(i==9)
                    mineNum=map->isMine(i,j-1)+map->isMine(i,j+1)+map->isMine(i-1,j+1)+map->isMine(i-1,j)+map->isMine(i-1,j-1);
                else if(j==0)
                    mineNum=map->isMine(i-1,j)+map->isMine(i+1,j)+map->isMine(i-1,j+1)+map->isMine(i,j+1)+map->isMine(i+1,j+1);
                else if(j==9)
                    mineNum=map->isMine(i-1,j)+map->isMine(i+1,j)+map->isMine(i-1,j-1)+map->isMine(i,j-1)+map->isMine(i+1,j-1);
                else
                    mineNum=map->isMine(i-1,j-1)+map->isMine(i-1,j)+map->isMine(i-1,j+1)
                            +map->isMine(i,j-1)+map->isMine(i,j+1)
                            +map->isMine(i+1,j-1)+map->isMine(i+1,j)+map->isMine(i+1,j+1);

                //设置按钮图标路径
                QString str = ":/unknown/res/"+QString::number(mineNum)+".png";
                uBtn = new UnknownPushButton(str);

            }
            uBtn->setISMine(map->isMine(i,j));
            uBtn->setParent(this);
            uBtn->move((this->width()-10*uBtn->width())*0.5+j*uBtn->width(),
                       this->height()*0.2+i*uBtn->height());
            uBtn->setX(i);
            uBtn->setY(j);
            uBtn->setIsTurn(true);

            this->mineBoard[i][j]=uBtn;//保存生成的按键

            //如果按到炸弹按钮，触发失败信号，游戏失败，展示雷的位置，禁用所有按钮,计时归零
            connect(uBtn,&UnknownPushButton::youLose,[=](){
                for(int x = 0;x < 10;x++)
                {
                    for(int y = 0; y < 10;y++)
                    {
                        this->showMine();
                        this->mineBoard[x][y]->setIsTurn(true);
                    }
                }
                startBtn->setIcon(QIcon(":/res/sad.png"));
                qtime->stop();
                this->m_time = 0;
            });


            connect(uBtn,&UnknownPushButton::isZero,[=](){
                this->turnOtherUnknownPushButton(i,j);

                this->Win();
            });

            connect(uBtn,&UnknownPushButton::isGuess,[=](){
                this->Win();
            });

        }
    }


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::turnUpUnknownPushButton(int x,int y)
{
    if(x-1>-1)
    {
        if(false == this->mineBoard[x-1][y]->getIsTurn())
        {
            this->mineBoard[x-1][y]->turnBtn();
            if(this->mineBoard[x-1][y]->getNumPath()==":/unknown/res/0.png")
            {
                this->turnUpUnknownPushButton(x-1,y);
                this->turnLeftUnknownPushButton(x,y);
                this->turnRightUnknownPushButton(x,y);
            }
        }
    }
}

void MainWindow::turnLeftUnknownPushButton(int x,int y)
{
    if(y-1>-1)
    {
        if(false == this->mineBoard[x][y-1]->getIsTurn())
        {
            this->mineBoard[x][y-1]->turnBtn();
            if(this->mineBoard[x][y-1]->getNumPath()==":/unknown/res/0.png")
            {
                this->turnLeftUnknownPushButton(x,y-1);
                this->turnUpUnknownPushButton(x,y-1);
                this->turnDownUnknownPushButton(x,y-1);
            }
        }
    }
}

void MainWindow::turnRightUnknownPushButton(int x,int y)
{
    if(y+1<10)
    {
        if(false == this->mineBoard[x][y+1]->getIsTurn())
        {
            this->mineBoard[x][y+1]->turnBtn();
            if(this->mineBoard[x][y+1]->getNumPath()==":/unknown/res/0.png")
            {
                this->turnRightUnknownPushButton(x,y+1);
                this->turnUpUnknownPushButton(x,y+1);
                this->turnDownUnknownPushButton(x,y+1);
            }
        }
    }
}

void MainWindow::turnDownUnknownPushButton(int x, int y)
{
    if(x+1<10)
    {
        if(false == this->mineBoard[x+1][y]->getIsTurn())
        {
            this->mineBoard[x+1][y]->turnBtn();
            if(this->mineBoard[x+1][y]->getNumPath()==":/unknown/res/0.png")
            {
                this->turnDownUnknownPushButton(x+1,y);
                this->turnLeftUnknownPushButton(x,y);
                this->turnRightUnknownPushButton(x,y);
            }
        }
    }
}

void MainWindow::turnOtherUnknownPushButton(int x,int y)
{
    this->turnUpUnknownPushButton(x,y);
    this->turnLeftUnknownPushButton(x,y);
    this->turnRightUnknownPushButton(x,y);
    this->turnDownUnknownPushButton(x,y);
}

void MainWindow::showMine(void)
{
    for(int i = 0;i < 10; i++)
    {
        for(int j = 0;j <10;j++)
        {
            if(true == this->mineBoard[i][j]->getIsMine())
            {
                this->mineBoard[i][j]->setIcon(QIcon(mineBoard[i][j]->getNumPath()));
            }
        }
    }
}

bool MainWindow::isWin(void)
{
    int Num = 0;
    bool ret = false;
    for(int i = 0;i < 10; i++)
    {
        for(int j = 0;j <10;j++)
        {
            if(true == this->mineBoard[i][j]->getIsMine() && true == this->mineBoard[i][j]->getIsGuess())
            {
                Num++;
//                qDebug()<<Num;
            }
        }
    }
    if(Num == 10)
        ret = true;
    return ret;
}

void MainWindow::Win(void)
{
    if(true == this->isWin())
    {
//        qDebug()<<"win";
                    for(int x = 0;x < 10;x++)
                    {
                        for(int y = 0; y < 10;y++)
                        {
                            this->mineBoard[x][y]->setIsTurn(true);
                        }
                    }
        startBtn->setIcon(QIcon(":/res/celebrate.png"));
        qtime->stop();

        recodeWidget = new RecodeWidget(this->m_time);
        recodeWidget->show();

    }
}

